<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    温州理工<br>
    学号 24219141131<br>
    20250415<br>
    
    <!-- TodoList 实现 -->
    <div class="todo-container">
      <h2>Todo List</h2>
      <div class="add-todo">
        <input 
          v-model="newTodoTitle" 
          @keyup.enter="addTodo"
          placeholder="输入待办事项..."
        >
        <button @click="addTodo">添加</button>
      </div>
      
      <ul class="todo-list">
        <TodoItem 
          v-for="(todo, index) in todos" 
          :key="index"
          :title="todo.title"
          :completed="todo.completed"
          @update:completed="updateTodoCompleted(index, $event)"
          @remove-todo="removeTodo(index)"
        />
      </ul>
    </div>
  </div>
</template>

<script>
import TodoItem from './TodoItem.vue'

export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  components: {
    TodoItem
  },
  data() {
    return {
      todos: [
        { title: '学习Vue', completed: false },
        { title: '完成实验8', completed: false }
      ],
      newTodoTitle: ''
    }
  },
  methods: {
    addTodo() {
      if (this.newTodoTitle.trim()) {
        this.todos.push({
          title: this.newTodoTitle.trim(),
          completed: false
        });
        this.newTodoTitle = '';
      }
    },
    removeTodo(index) {
      this.todos.splice(index, 1);
    },
    updateTodoCompleted(index, completed) {
      this.todos[index].completed = completed;
    }
  }
}
</script>

<style scoped>
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}

/* TodoList 样式 */
.todo-container {
  max-width: 500px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  background-color: #f9f9f9;
}

.add-todo {
  display: flex;
  margin-bottom: 20px;
}

.add-todo input {
  flex-grow: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
  margin-right: 10px;
}

.add-todo button {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.add-todo button:hover {
  background-color: #369f6e;
}

.todo-list {
  text-align: left;
}
</style>